import type {
  ConsultOrderPreParams,
  DoctorPage,
  FollowType,
  Image,
  KnowledgePage,
  KnowledgeParams,
  PageParams,
  TopDep,
  ConsultOrderPreData,
  PartialConsult,
  OrderUrlParams,
  ConsultOrderItem,
  ConsultOrderListParams,
  ConsultOrderPage
} from '@/types/consult'
import type { EvaluateOrder } from '@/types/room'
import { request } from '@/utils/request'

export const getKnowledgePage = (params: KnowledgeParams) =>
  request<KnowledgePage>('/patient/home/knowledge', 'GET', params)

export const getDoctorPage = (params: PageParams) =>
  request<DoctorPage>('/home/page/doc', 'GET', params)

export const followOrUnfollow = (type: FollowType, id: string) =>
  request<{ id: string }>('/like', 'POST', { type, id })

export const getAllDep = () => request<TopDep[]>('/dep/all')

export const upload = (file: File) => {
  const fd = new FormData()
  fd.append('file', file)
  return request<Image>('/upload', 'POST', fd)
}

// 获取订单预支付信息
export const getConsultOrderPre = (params: ConsultOrderPreParams) =>
  request<ConsultOrderPreData>('/patient/consult/order/pre', 'GET', params)

// 根据问诊记录获取订单id
export const createConsultOrder = (data: PartialConsult) =>
  request<{ id: string }>('/patient/consult/order', 'POST', data)

// 生成支付地址的 API 函数
export const getConsultOrderPayUrl = (params: OrderUrlParams) =>
  request<{ payUrl: string }>('/patient/consult/pay', 'POST', params)

// 获取订单的详情对象(根据订单id)
export const getConsultOrderDetail = (orderId: string) =>
  request<ConsultOrderItem>('/patient/consult/order/detail', 'GET', { orderId })

// 获取原始的处方单
export const getPrescriptionPic = (id: string) =>
  request<Image>(`/patient/consult/prescription/${id}`)

// 评价医生
export const evaluateConsultOrder = (data: EvaluateOrder) =>
  request<{ id: string }>('/patient/order/evaluate', 'POST', data)

// 获取问诊记录列表
export const getConsultOrderList = (param: ConsultOrderListParams) =>
  request<ConsultOrderPage>('/patient/consult/order/list', 'GET', param)

// 取消问诊订单
export const cancelOrder = (id: string) =>
  request(`/patient/order/cancel/${id}`, 'PUT')

// 删除订单
export const deleteOrder = (id: string) =>
  request(`/patient/order/${id}`, 'DELETE')
